package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.StoryArticle;
import com.addplus.townmall.server.api.model.town.expand.SolrModelExpand;
import com.addplus.townmall.server.api.model.town.expand.StoryArticleAllExpand;
import com.addplus.townmall.server.api.model.town.expand.StoryArticleExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * <p>
 * 商城文章详情 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface StoryArticleMapper extends BaseAddMapper<StoryArticle> {


    @Select("SELECT\n" +
            "\ts.id,\n" +
            "\ts.title,\n" +
            "\ts.context,\n" +
            "\ts.img_thumb,\n" +
            "\ts.img,\n" +
            "\ts.story_label_id,\n" +
            "\tl.label_name,\n" +
            "\tl.label_img,\n" +
            "\ts.story_author_id,\n" +
            "\ta.author_name,\n" +
            "\ta.avatar_img,\n" +
            "\ts.shop_id_all,\n" +
            "\ts.readcount + s.readcount_true AS readcount,\n" +
            "\ts.readcount_true,\n" +
            "\ts.collection + s.collection_true AS collection_true,\n" +
            "\ts.`comment` + s.comment_true AS comment_true,\n" +
            "\ts.`share` + s.share_true AS share_true,\n" +
            "\ts.gmt_create\n" +
            "FROM\n" +
            "\tstory_article s LEFT JOIN story_author a ON a.id = s.story_author_id AND a.is_delete = 0\n" +
            "\tLEFT JOIN story_label l ON l.id = s.story_label_id AND l.is_delete = 0\n" +
            "WHERE\n" +
            "\ts.id = #{storyArticeId} \n" +
            "\tAND s.is_delete =0")
    @Results({
            //映射内嵌实体类
            @Result(column = "id", property = "storyArticle.id"),
            @Result(column = "title", property = "storyArticle.title"),
            @Result(column = "context", property = "storyArticle.context"),
            @Result(column = "img_thumb", property = "storyArticle.imgThumb"),
            @Result(column = "img", property = "storyArticle.img"),
            @Result(column = "story_label_id", property = "storyArticle.storyLabelId"),
            @Result(column = "story_author_id", property = "storyArticle.storyAuthorId"),
            @Result(column = "shop_id_all", property = "storyArticle.shopIdAll"),
            @Result(column = "readcount", property = "storyArticle.readCount"),
            @Result(column = "readcount_true", property = "storyArticle.readCountTrue"),
            @Result(column = "collection_true", property = "storyArticle.collectionTrue"),
            @Result(column = "comment_true", property = "storyArticle.commentTrue"),
            @Result(column = "share_true", property = "storyArticle.shareTrue"),
            @Result(column = "gmt_create", property = "storyArticle.gmtCreate"),
            @Result(column = "author_name", property = "storyAuthor.authorName"),
            @Result(column = "avatar_img", property = "storyAuthor.avatarImg"),
            @Result(column = "label_name", property = "storyLabel.labelName"),
            @Result(column = "label_img", property = "storyLabel.labelImg"),
    })
    StoryArticleAllExpand getStoryArticleExpand(@Param("storyArticeId") Integer storyArticeId);

//     @Select("select ar.*,ca.`name`,la.label_name,au.author_name\t \n" +
//             "from story_article ar\n" +
//             "LEFT JOIN story_category ca ON ca.id = ar.story_category_id AND ca.is_delete = 0\n" +
//             "LEFT JOIN story_label la ON la.id = ar.story_label_id AND la.is_delete = 0\n" +
//             "LEFT JOIN story_author au ON au.id = ar.story_author_id AND au.is_delete = 0\n" +
//             "WHERE ar.is_delete = 0")
     @Select("SELECT ar.id,ar.title,ar.img,ar.story_category_id,ar.story_author_id,story_label_id,\n" +
             "ar.readcount_true,ar.likecount_true,ar.collection_true,ar.share_true,ar.comment_true,ar.`online`,ar.is_sort,ar.gmt_create,ar.gmt_modified,\n" +
             "ca.`name`,la.label_name,au.author_name\n" +
             "FROM story_article ar\n" +
             "LEFT JOIN story_category ca ON ca.id = ar.story_category_id AND ca.is_delete = 0\n" +
             "LEFT JOIN story_label la ON la.id = ar.story_label_id AND la.is_delete = 0\n" +
             "LEFT JOIN story_author au ON au.id = ar.story_author_id AND au.is_delete = 0\n" +
             "WHERE ar.is_delete = 0 order by is_sort")
     List<StoryArticleExpand> getStoryAriticleExpandWeb(Page page);

//     @Select("select ar.*,ca.`name`,la.label_name,au.author_name\t \n" +
//             "from story_article ar\n" +
//             "LEFT JOIN story_category ca ON ca.id = ar.story_category_id AND ca.is_delete = 0\n" +
//             "LEFT JOIN story_label la ON la.id = ar.story_label_id AND la.is_delete = 0\n" +
//             "LEFT JOIN story_author au ON au.id = ar.story_author_id AND au.is_delete = 0\n" +
//             "WHERE ar.is_delete = 0 AND ar.title like '%${title}%'")
     @Select("SELECT ar.id,ar.title,ar.img,ar.story_category_id,ar.story_author_id,story_label_id,\n" +
             "ar.readcount_true,ar.likecount_true,ar.collection_true,ar.share_true,ar.comment_true,ar.is_sort,ar.gmt_create,ar.gmt_modified,\n" +
             "ca.`name`,la.label_name,au.author_name\n" +
             "FROM story_article ar\n" +
             "LEFT JOIN story_category ca ON ca.id = ar.story_category_id AND ca.is_delete = 0\n" +
             "LEFT JOIN story_label la ON la.id = ar.story_label_id AND la.is_delete = 0\n" +
             "LEFT JOIN story_author au ON au.id = ar.story_author_id AND au.is_delete = 0\n" +
             "WHERE ar.is_delete = 0 AND ar.title like '%${title}%' order by is_sort")
     List<StoryArticleExpand> getStoryAriticleExpandByNameWeb(@Param("page") Page page,@Param("title") String title);

     @Select("select ar.*,ca.`name`,la.label_name,au.author_name\t \n" +
             "from story_article ar\n" +
             "LEFT JOIN story_category ca ON ca.id = ar.story_category_id AND ca.is_delete = 0\n" +
             "LEFT JOIN story_label la ON la.id = ar.story_label_id AND la.is_delete = 0\n" +
             "LEFT JOIN story_author au ON au.id = ar.story_author_id AND au.is_delete = 0\n" +
             "WHERE ar.is_delete = 0 AND ar.id = #{id}")
     StoryArticleExpand getStoryArticleExpandById(@Param("id") Integer id);

}
